/*
 * Copyright (C) 2021 The Chinese Software International Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tonyodev.fetch2.database;

import ohos.data.rdb.RdbPredicates;
import ohos.data.rdb.RdbStore;
import ohos.data.rdb.ValuesBucket;
import ohos.data.resultset.ResultSet;
import ohos.utils.zson.ZSONObject;

import com.tonyodev.fetch2.EnqueueAction;
import com.tonyodev.fetch2.Error;
import com.tonyodev.fetch2.Priority;
import com.tonyodev.fetch2.Status;
import com.tonyodev.fetch2core.Extras;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @since 2021-05-26
 */
public class DownloadDao_Impl implements DownloadDao {

    private String TAG = "lpq " + DownloadDao_Impl.class.getSimpleName();

    private RdbStore database;

    private DownloadDao_Impl() {
    }

    public DownloadDao_Impl(RdbStore database) {
        this.database = database;
    }


    @Override
    public long insert(DownloadInfo downloadInfo) {
        return database.insert(DownloadDatabase.TABLE_NAME, DownloadDatabase.downloadInfo2ValuesBucket(downloadInfo));
    }

    @Override
    public List<Long> insert(List<DownloadInfo> downloadInfoList) {
        ArrayList<Long> list = new ArrayList<>();
        for (DownloadInfo info : downloadInfoList) {
            long user = database.insert(DownloadDatabase.TABLE_NAME, DownloadDatabase.downloadInfo2ValuesBucket(info));
            list.add(user);
        }
        return list;
    }

    @Override
    public void delete(DownloadInfo downloadInfo) {
        database.executeSql("delete from " + DownloadDatabase.TABLE_NAME + " where " + DownloadDatabase.COLUMN_ID + " = " + downloadInfo.getId());
    }

    @Override
    public void delete(List<DownloadInfo> downloadInfoList) {
        for (DownloadInfo info : downloadInfoList) {
            database.executeSql("delete from " + DownloadDatabase.TABLE_NAME + " where " + DownloadDatabase.COLUMN_ID + " = " + info.getId());
        }
    }

    @Override
    public void deleteAll() {
        database.executeSql("delete from " + DownloadDatabase.TABLE_NAME);
    }

    @Override
    public void update(DownloadInfo download) {
        delete(download);
        insert(download);
    }

    @Override
    public void update(List<DownloadInfo> downloadInfoList) {
        for (DownloadInfo info : downloadInfoList) {
            delete(info);
            insert(info);
        }
    }

    @Override
    public List<DownloadInfo> get() {
        String[] columns = new String[]{DownloadDatabase.COLUMN_ID, DownloadDatabase.COLUMN_NAMESPACE
            , DownloadDatabase.COLUMN_URL, DownloadDatabase.COLUMN_FILE, DownloadDatabase.COLUMN_GROUP
            , DownloadDatabase.COLUMN_PRIORITY, DownloadDatabase.COLUMN_HEADERS, DownloadDatabase.COLUMN_DOWNLOADED
            , DownloadDatabase.COLUMN_TOTAL, DownloadDatabase.COLUMN_STATUS, DownloadDatabase.COLUMN_ERROR
            , DownloadDatabase.COLUMN_NETWORK_TYPE, DownloadDatabase.COLUMN_CREATED, DownloadDatabase.COLUMN_TAG
            , DownloadDatabase.COLUMN_ENQUEUE_ACTION, DownloadDatabase.COLUMN_IDENTIFIER, DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE,
            DownloadDatabase.COLUMN_EXTRAS, DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS, DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS};
        RdbPredicates rdbPredicates = new RdbPredicates(DownloadDatabase.TABLE_NAME);//构建查询谓词
        ResultSet resultSet = database.query(rdbPredicates, columns);
        ArrayList<DownloadInfo> downloadInfos = new ArrayList<>();
        while (resultSet.goToNextRow()) {
            int column_id = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ID));
            String column_namespace = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_NAMESPACE));
            String COLUMN_URL = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_URL));
            String COLUMN_FILE = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_FILE));
            int COLUMN_GROUP = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_GROUP));
            int COLUMN_PRIORITY = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_PRIORITY));
            String COLUMN_HEADERS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_HEADERS));
            long COLUMN_DOWNLOADED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOADED));
            long COLUMN_TOTAL = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TOTAL));
            int COLUMN_STATUS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_STATUS));
            int COLUMN_ERROR = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ERROR));
            long COLUMN_CREATED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_CREATED));
            String COLUMN_TAG = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TAG));
            int COLUMN_ENQUEUE_ACTION = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ENQUEUE_ACTION));
            long COLUMN_IDENTIFIER = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_IDENTIFIER));
            int COLUMN_DOWNLOAD_ON_ENQUEUE = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE));
            String COLUMN_EXTRAS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_EXTRAS));
            int COLUMN_AUTO_RETRY_MAX_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS));
            int COLUMN_AUTO_RETRY_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS));

            System.out.println(TAG + " COLUMN_EXTRAS = " + COLUMN_EXTRAS);
            DownloadInfo downloadInfo = new DownloadInfo();
            downloadInfo.setId(column_id);
            downloadInfo.setNamespace(column_namespace);
            downloadInfo.setUrl(COLUMN_URL);
            downloadInfo.setFile(COLUMN_FILE);
            downloadInfo.setGroup(COLUMN_GROUP);
            downloadInfo.setPriority(Priority.valueOf(COLUMN_PRIORITY));
            if (COLUMN_HEADERS != null && !COLUMN_HEADERS.equals("")) {
                HashMap<String, String> headers = new HashMap<>();
                String[] split = COLUMN_HEADERS.split(DownloadDatabase.CUTTING_LINE);
                for (int i = 0; i < split.length; i++) {
                    String content = split[i];
                    String[] split1 = content.split(DownloadDatabase.CUTTING_LINE_KEY);
                    String key = split1[0];
                    String value = split1[1];
                    headers.put(key, value);
                }
                downloadInfo.setHeaders(headers);
            }
            downloadInfo.setDownloaded(COLUMN_DOWNLOADED);
            downloadInfo.setTotal(COLUMN_TOTAL);
            downloadInfo.setStatus(Status.valueOf(COLUMN_STATUS));
            downloadInfo.setError(Error.valueOf(COLUMN_ERROR));
            downloadInfo.setCreated(COLUMN_CREATED);
            downloadInfo.setTag(COLUMN_TAG);
            downloadInfo.setEnqueueAction(EnqueueAction.valueOf(COLUMN_ENQUEUE_ACTION));
            downloadInfo.setIdentifier(COLUMN_IDENTIFIER);
            if (COLUMN_DOWNLOAD_ON_ENQUEUE == 1) {
                downloadInfo.setDownloadOnEnqueue(true);
            } else {
                downloadInfo.setDownloadOnEnqueue(false);
            }
            downloadInfo.setAutoRetryMaxAttempts(COLUMN_AUTO_RETRY_MAX_ATTEMPTS);
            downloadInfo.setAutoRetryAttempts(COLUMN_AUTO_RETRY_ATTEMPTS);
            if (COLUMN_EXTRAS != null && !COLUMN_EXTRAS.equals("")) {
                ZSONObject zsonObject = ZSONObject.stringToZSON(COLUMN_EXTRAS);
                Set<String> list = zsonObject.keySet();
                HashMap<String, String> new_extras = new HashMap<>();
                for (String key : list) {
                    String value = (String) zsonObject.get(key);
                    new_extras.put(key, value);
                }
                downloadInfo.setExtras(new Extras(new_extras));
            }

            downloadInfos.add(downloadInfo);
        }
        resultSet.close();
        return downloadInfos;
    }

    @Override
    public DownloadInfo get(int id) {
        String[] columns = new String[]{DownloadDatabase.COLUMN_ID, DownloadDatabase.COLUMN_NAMESPACE
            , DownloadDatabase.COLUMN_URL, DownloadDatabase.COLUMN_FILE, DownloadDatabase.COLUMN_GROUP
            , DownloadDatabase.COLUMN_PRIORITY, DownloadDatabase.COLUMN_HEADERS, DownloadDatabase.COLUMN_DOWNLOADED
            , DownloadDatabase.COLUMN_TOTAL, DownloadDatabase.COLUMN_STATUS, DownloadDatabase.COLUMN_ERROR
            , DownloadDatabase.COLUMN_NETWORK_TYPE, DownloadDatabase.COLUMN_CREATED, DownloadDatabase.COLUMN_TAG
            , DownloadDatabase.COLUMN_ENQUEUE_ACTION, DownloadDatabase.COLUMN_IDENTIFIER, DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE,
            DownloadDatabase.COLUMN_EXTRAS, DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS, DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS};
        RdbPredicates rdbPredicates = new RdbPredicates(DownloadDatabase.TABLE_NAME);//构建查询谓词
        ResultSet resultSet = database.query(rdbPredicates, columns);
        while (resultSet.goToNextRow()) {
            int column_id = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ID));
            String column_namespace = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_NAMESPACE));
            String COLUMN_URL = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_URL));
            String COLUMN_FILE = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_FILE));
            int COLUMN_GROUP = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_GROUP));
            int COLUMN_PRIORITY = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_PRIORITY));
            String COLUMN_HEADERS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_HEADERS));
            long COLUMN_DOWNLOADED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOADED));
            long COLUMN_TOTAL = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TOTAL));
            int COLUMN_STATUS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_STATUS));
            int COLUMN_ERROR = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ERROR));
            long COLUMN_CREATED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_CREATED));
            String COLUMN_TAG = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TAG));
            int COLUMN_ENQUEUE_ACTION = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ENQUEUE_ACTION));
            long COLUMN_IDENTIFIER = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_IDENTIFIER));
            int COLUMN_DOWNLOAD_ON_ENQUEUE = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE));
            String COLUMN_EXTRAS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_EXTRAS));
            int COLUMN_AUTO_RETRY_MAX_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS));
            int COLUMN_AUTO_RETRY_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS));

            if (id == column_id) {
                DownloadInfo downloadInfo = new DownloadInfo();
                downloadInfo.setId(column_id);
                downloadInfo.setNamespace(column_namespace);
                downloadInfo.setUrl(COLUMN_URL);
                downloadInfo.setFile(COLUMN_FILE);
                downloadInfo.setGroup(COLUMN_GROUP);
                downloadInfo.setPriority(Priority.valueOf(COLUMN_PRIORITY));
                if (COLUMN_HEADERS != null && !COLUMN_HEADERS.equals("")) {
                    HashMap<String, String> headers = new HashMap<>();
                    String[] split = COLUMN_HEADERS.split(DownloadDatabase.CUTTING_LINE);
                    for (int i = 0; i < split.length; i++) {
                        String content = split[i];
                        String[] split1 = content.split(DownloadDatabase.CUTTING_LINE_KEY);
                        String key = split1[0];
                        String value = split1[1];
                        headers.put(key, value);
                    }
                    downloadInfo.setHeaders(headers);
                }
                downloadInfo.setDownloaded(COLUMN_DOWNLOADED);
                downloadInfo.setTotal(COLUMN_TOTAL);
                downloadInfo.setStatus(Status.valueOf(COLUMN_STATUS));
                downloadInfo.setError(Error.valueOf(COLUMN_ERROR));
                downloadInfo.setCreated(COLUMN_CREATED);
                downloadInfo.setTag(COLUMN_TAG);
                downloadInfo.setEnqueueAction(EnqueueAction.valueOf(COLUMN_ENQUEUE_ACTION));
                downloadInfo.setIdentifier(COLUMN_IDENTIFIER);
                if (COLUMN_DOWNLOAD_ON_ENQUEUE == 1) {
                    downloadInfo.setDownloadOnEnqueue(true);
                } else {
                    downloadInfo.setDownloadOnEnqueue(false);
                }
                downloadInfo.setAutoRetryMaxAttempts(COLUMN_AUTO_RETRY_MAX_ATTEMPTS);
                downloadInfo.setAutoRetryAttempts(COLUMN_AUTO_RETRY_ATTEMPTS);
                if (COLUMN_EXTRAS != null && !COLUMN_EXTRAS.equals("")) {
                    ZSONObject zsonObject = ZSONObject.stringToZSON(COLUMN_EXTRAS);
                    Set<String> list = zsonObject.keySet();
                    HashMap<String, String> new_extras = new HashMap<>();
                    for (String key : list) {
                        String value = (String) zsonObject.get(key);
                        new_extras.put(key, value);
                    }
                    downloadInfo.setExtras(new Extras(new_extras));
                }
                resultSet.close();
                return downloadInfo;
            }
        }
        return null;
    }

    @Override
    public List<DownloadInfo> get(List<Integer> ids) {
        ArrayList<DownloadInfo> downloads = new ArrayList<>();
        for (Integer info : ids) {
            DownloadInfo downloadInfo = get(info);
            if (downloadInfo != null) {
                downloads.add(downloadInfo);
            }
        }
        return downloads;
    }

    @Override
    public DownloadInfo getByFile(String file) {
        String[] columns = new String[]{DownloadDatabase.COLUMN_ID, DownloadDatabase.COLUMN_NAMESPACE
            , DownloadDatabase.COLUMN_URL, DownloadDatabase.COLUMN_FILE, DownloadDatabase.COLUMN_GROUP
            , DownloadDatabase.COLUMN_PRIORITY, DownloadDatabase.COLUMN_HEADERS, DownloadDatabase.COLUMN_DOWNLOADED
            , DownloadDatabase.COLUMN_TOTAL, DownloadDatabase.COLUMN_STATUS, DownloadDatabase.COLUMN_ERROR
            , DownloadDatabase.COLUMN_NETWORK_TYPE, DownloadDatabase.COLUMN_CREATED, DownloadDatabase.COLUMN_TAG
            , DownloadDatabase.COLUMN_ENQUEUE_ACTION, DownloadDatabase.COLUMN_IDENTIFIER, DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE,
            DownloadDatabase.COLUMN_EXTRAS, DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS, DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS};
        RdbPredicates rdbPredicates = new RdbPredicates(DownloadDatabase.TABLE_NAME);//构建查询谓词
        ResultSet resultSet = database.query(rdbPredicates, columns);
        while (resultSet.goToNextRow()) {
            int column_id = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ID));
            String column_namespace = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_NAMESPACE));
            String COLUMN_URL = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_URL));
            String COLUMN_FILE = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_FILE));
            int COLUMN_GROUP = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_GROUP));
            int COLUMN_PRIORITY = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_PRIORITY));
            String COLUMN_HEADERS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_HEADERS));
            long COLUMN_DOWNLOADED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOADED));
            long COLUMN_TOTAL = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TOTAL));
            int COLUMN_STATUS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_STATUS));
            int COLUMN_ERROR = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ERROR));
            long COLUMN_CREATED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_CREATED));
            String COLUMN_TAG = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TAG));
            int COLUMN_ENQUEUE_ACTION = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ENQUEUE_ACTION));
            long COLUMN_IDENTIFIER = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_IDENTIFIER));
            int COLUMN_DOWNLOAD_ON_ENQUEUE = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE));
            String COLUMN_EXTRAS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_EXTRAS));
            int COLUMN_AUTO_RETRY_MAX_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS));
            int COLUMN_AUTO_RETRY_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS));
            if (file.equals(COLUMN_FILE)) {
                DownloadInfo downloadInfo = new DownloadInfo();
                downloadInfo.setId(column_id);
                downloadInfo.setNamespace(column_namespace);
                downloadInfo.setUrl(COLUMN_URL);
                downloadInfo.setFile(COLUMN_FILE);
                downloadInfo.setGroup(COLUMN_GROUP);
                downloadInfo.setPriority(Priority.valueOf(COLUMN_PRIORITY));
                if (COLUMN_HEADERS != null && !COLUMN_HEADERS.equals("")) {
                    HashMap<String, String> headers = new HashMap<>();
                    String[] split = COLUMN_HEADERS.split(DownloadDatabase.CUTTING_LINE);
                    for (int i = 0; i < split.length; i++) {
                        String content = split[i];
                        String[] split1 = content.split(DownloadDatabase.CUTTING_LINE_KEY);
                        String key = split1[0];
                        String value = split1[1];
                        headers.put(key, value);
                    }
                    downloadInfo.setHeaders(headers);
                }
                downloadInfo.setDownloaded(COLUMN_DOWNLOADED);
                downloadInfo.setTotal(COLUMN_TOTAL);
                downloadInfo.setStatus(Status.valueOf(COLUMN_STATUS));
                downloadInfo.setError(Error.valueOf(COLUMN_ERROR));
                downloadInfo.setCreated(COLUMN_CREATED);
                downloadInfo.setTag(COLUMN_TAG);
                downloadInfo.setEnqueueAction(EnqueueAction.valueOf(COLUMN_ENQUEUE_ACTION));
                downloadInfo.setIdentifier(COLUMN_IDENTIFIER);
                if (COLUMN_DOWNLOAD_ON_ENQUEUE == 1) {
                    downloadInfo.setDownloadOnEnqueue(true);
                } else {
                    downloadInfo.setDownloadOnEnqueue(false);
                }
                downloadInfo.setAutoRetryMaxAttempts(COLUMN_AUTO_RETRY_MAX_ATTEMPTS);
                downloadInfo.setAutoRetryAttempts(COLUMN_AUTO_RETRY_ATTEMPTS);
                if (COLUMN_EXTRAS != null && !COLUMN_EXTRAS.equals("")) {
                    ZSONObject zsonObject = ZSONObject.stringToZSON(COLUMN_EXTRAS);
                    Set<String> list = zsonObject.keySet();
                    HashMap<String, String> new_extras = new HashMap<>();
                    for (String key : list) {
                        String value = (String) zsonObject.get(key);
                        new_extras.put(key, value);
                    }
                    downloadInfo.setExtras(new Extras(new_extras));
                }
                resultSet.close();
                return downloadInfo;
            }
        }
        resultSet.close();
        return null;
    }

    @Override
    public List<DownloadInfo> getByStatus(Status status) {
        String[] columns = new String[]{DownloadDatabase.COLUMN_ID, DownloadDatabase.COLUMN_NAMESPACE
            , DownloadDatabase.COLUMN_URL, DownloadDatabase.COLUMN_FILE, DownloadDatabase.COLUMN_GROUP
            , DownloadDatabase.COLUMN_PRIORITY, DownloadDatabase.COLUMN_HEADERS, DownloadDatabase.COLUMN_DOWNLOADED
            , DownloadDatabase.COLUMN_TOTAL, DownloadDatabase.COLUMN_STATUS, DownloadDatabase.COLUMN_ERROR
            , DownloadDatabase.COLUMN_NETWORK_TYPE, DownloadDatabase.COLUMN_CREATED, DownloadDatabase.COLUMN_TAG
            , DownloadDatabase.COLUMN_ENQUEUE_ACTION, DownloadDatabase.COLUMN_IDENTIFIER, DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE,
            DownloadDatabase.COLUMN_EXTRAS, DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS, DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS};
        RdbPredicates rdbPredicates = new RdbPredicates(DownloadDatabase.TABLE_NAME);//构建查询谓词
        ResultSet resultSet = database.query(rdbPredicates, columns);
        ArrayList<DownloadInfo> downloadInfos = new ArrayList<>();
        while (resultSet.goToNextRow()) {
            int column_id = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ID));
            String column_namespace = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_NAMESPACE));
            String COLUMN_URL = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_URL));
            String COLUMN_FILE = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_FILE));
            int COLUMN_GROUP = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_GROUP));
            int COLUMN_PRIORITY = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_PRIORITY));
            String COLUMN_HEADERS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_HEADERS));
            long COLUMN_DOWNLOADED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOADED));
            long COLUMN_TOTAL = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TOTAL));
            int COLUMN_STATUS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_STATUS));
            int COLUMN_ERROR = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ERROR));
            long COLUMN_CREATED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_CREATED));
            String COLUMN_TAG = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TAG));
            int COLUMN_ENQUEUE_ACTION = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ENQUEUE_ACTION));
            long COLUMN_IDENTIFIER = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_IDENTIFIER));
            int COLUMN_DOWNLOAD_ON_ENQUEUE = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE));
            String COLUMN_EXTRAS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_EXTRAS));
            int COLUMN_AUTO_RETRY_MAX_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS));
            int COLUMN_AUTO_RETRY_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS));
            if (status.getValue() == COLUMN_STATUS) {
                DownloadInfo downloadInfo = new DownloadInfo();
                downloadInfo.setId(column_id);
                downloadInfo.setNamespace(column_namespace);
                downloadInfo.setUrl(COLUMN_URL);
                downloadInfo.setFile(COLUMN_FILE);
                downloadInfo.setGroup(COLUMN_GROUP);
                downloadInfo.setPriority(Priority.valueOf(COLUMN_PRIORITY));
                if (COLUMN_HEADERS != null && !COLUMN_HEADERS.equals("")) {
                    HashMap<String, String> headers = new HashMap<>();
                    String[] split = COLUMN_HEADERS.split(DownloadDatabase.CUTTING_LINE);
                    for (int i = 0; i < split.length; i++) {
                        String content = split[i];
                        String[] split1 = content.split(DownloadDatabase.CUTTING_LINE_KEY);
                        String key = split1[0];
                        String value = split1[1];
                        headers.put(key, value);
                    }
                    downloadInfo.setHeaders(headers);
                }
                downloadInfo.setDownloaded(COLUMN_DOWNLOADED);
                downloadInfo.setTotal(COLUMN_TOTAL);
                downloadInfo.setStatus(Status.valueOf(COLUMN_STATUS));
                downloadInfo.setError(Error.valueOf(COLUMN_ERROR));
                downloadInfo.setCreated(COLUMN_CREATED);
                downloadInfo.setTag(COLUMN_TAG);
                downloadInfo.setEnqueueAction(EnqueueAction.valueOf(COLUMN_ENQUEUE_ACTION));
                downloadInfo.setIdentifier(COLUMN_IDENTIFIER);
                if (COLUMN_DOWNLOAD_ON_ENQUEUE == 1) {
                    downloadInfo.setDownloadOnEnqueue(true);
                } else {
                    downloadInfo.setDownloadOnEnqueue(false);
                }
                downloadInfo.setAutoRetryMaxAttempts(COLUMN_AUTO_RETRY_MAX_ATTEMPTS);
                downloadInfo.setAutoRetryAttempts(COLUMN_AUTO_RETRY_ATTEMPTS);
                if (COLUMN_EXTRAS != null && !COLUMN_EXTRAS.equals("")) {
                    ZSONObject zsonObject = ZSONObject.stringToZSON(COLUMN_EXTRAS);
                    Set<String> list = zsonObject.keySet();
                    HashMap<String, String> new_extras = new HashMap<>();
                    for (String key : list) {
                        String value = (String) zsonObject.get(key);
                        new_extras.put(key, value);
                    }
                    downloadInfo.setExtras(new Extras(new_extras));
                }
                downloadInfos.add(downloadInfo);
            }
        }
        resultSet.close();
        return downloadInfos;
    }

    @Override
    public List<DownloadInfo> getByStatus(List<Status> statuses) {
        String[] columns = new String[]{DownloadDatabase.COLUMN_ID, DownloadDatabase.COLUMN_NAMESPACE
            , DownloadDatabase.COLUMN_URL, DownloadDatabase.COLUMN_FILE, DownloadDatabase.COLUMN_GROUP
            , DownloadDatabase.COLUMN_PRIORITY, DownloadDatabase.COLUMN_HEADERS, DownloadDatabase.COLUMN_DOWNLOADED
            , DownloadDatabase.COLUMN_TOTAL, DownloadDatabase.COLUMN_STATUS, DownloadDatabase.COLUMN_ERROR
            , DownloadDatabase.COLUMN_NETWORK_TYPE, DownloadDatabase.COLUMN_CREATED, DownloadDatabase.COLUMN_TAG
            , DownloadDatabase.COLUMN_ENQUEUE_ACTION, DownloadDatabase.COLUMN_IDENTIFIER, DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE,
            DownloadDatabase.COLUMN_EXTRAS, DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS, DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS};
        RdbPredicates rdbPredicates = new RdbPredicates(DownloadDatabase.TABLE_NAME);//构建查询谓词
        ResultSet resultSet = database.query(rdbPredicates, columns);
        ArrayList<DownloadInfo> downloadInfos = new ArrayList<>();
        while (resultSet.goToNextRow()) {
            int column_id = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ID));
            String column_namespace = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_NAMESPACE));
            String COLUMN_URL = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_URL));
            String COLUMN_FILE = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_FILE));
            int COLUMN_GROUP = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_GROUP));
            int COLUMN_PRIORITY = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_PRIORITY));
            String COLUMN_HEADERS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_HEADERS));
            long COLUMN_DOWNLOADED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOADED));
            long COLUMN_TOTAL = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TOTAL));
            int COLUMN_STATUS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_STATUS));
            int COLUMN_ERROR = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ERROR));
            long COLUMN_CREATED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_CREATED));
            String COLUMN_TAG = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TAG));
            int COLUMN_ENQUEUE_ACTION = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ENQUEUE_ACTION));
            long COLUMN_IDENTIFIER = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_IDENTIFIER));
            int COLUMN_DOWNLOAD_ON_ENQUEUE = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE));
            String COLUMN_EXTRAS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_EXTRAS));
            int COLUMN_AUTO_RETRY_MAX_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS));
            int COLUMN_AUTO_RETRY_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS));
            for (Status status : statuses) {
                if (status.getValue() == COLUMN_STATUS) {
                    DownloadInfo downloadInfo = new DownloadInfo();
                    downloadInfo.setId(column_id);
                    downloadInfo.setNamespace(column_namespace);
                    downloadInfo.setUrl(COLUMN_URL);
                    downloadInfo.setFile(COLUMN_FILE);
                    downloadInfo.setGroup(COLUMN_GROUP);
                    downloadInfo.setPriority(Priority.valueOf(COLUMN_PRIORITY));
                    if (COLUMN_HEADERS != null && !COLUMN_HEADERS.equals("")) {
                        HashMap<String, String> headers = new HashMap<>();
                        String[] split = COLUMN_HEADERS.split(DownloadDatabase.CUTTING_LINE);
                        for (int i = 0; i < split.length; i++) {
                            String content = split[i];
                            String[] split1 = content.split(DownloadDatabase.CUTTING_LINE_KEY);
                            String key = split1[0];
                            String value = split1[1];
                            headers.put(key, value);
                        }
                        downloadInfo.setHeaders(headers);
                    }
                    downloadInfo.setDownloaded(COLUMN_DOWNLOADED);
                    downloadInfo.setTotal(COLUMN_TOTAL);
                    downloadInfo.setStatus(Status.valueOf(COLUMN_STATUS));
                    downloadInfo.setError(Error.valueOf(COLUMN_ERROR));
                    downloadInfo.setCreated(COLUMN_CREATED);
                    downloadInfo.setTag(COLUMN_TAG);
                    downloadInfo.setEnqueueAction(EnqueueAction.valueOf(COLUMN_ENQUEUE_ACTION));
                    downloadInfo.setIdentifier(COLUMN_IDENTIFIER);
                    if (COLUMN_DOWNLOAD_ON_ENQUEUE == 1) {
                        downloadInfo.setDownloadOnEnqueue(true);
                    } else {
                        downloadInfo.setDownloadOnEnqueue(false);
                    }
                    downloadInfo.setAutoRetryMaxAttempts(COLUMN_AUTO_RETRY_MAX_ATTEMPTS);
                    downloadInfo.setAutoRetryAttempts(COLUMN_AUTO_RETRY_ATTEMPTS);
                    if (COLUMN_EXTRAS != null && !COLUMN_EXTRAS.equals("")) {
                        ZSONObject zsonObject = ZSONObject.stringToZSON(COLUMN_EXTRAS);
                        Set<String> list = zsonObject.keySet();
                        HashMap<String, String> new_extras = new HashMap<>();
                        for (String key : list) {
                            String value = (String) zsonObject.get(key);
                            new_extras.put(key, value);
                        }
                        downloadInfo.setExtras(new Extras(new_extras));
                    }
                    downloadInfos.add(downloadInfo);
                }
            }
        }
        resultSet.close();
        return downloadInfos;
    }

    @Override
    public List<DownloadInfo> getByGroup(int group) {
        String[] columns = new String[]{DownloadDatabase.COLUMN_ID, DownloadDatabase.COLUMN_NAMESPACE
            , DownloadDatabase.COLUMN_URL, DownloadDatabase.COLUMN_FILE, DownloadDatabase.COLUMN_GROUP
            , DownloadDatabase.COLUMN_PRIORITY, DownloadDatabase.COLUMN_HEADERS, DownloadDatabase.COLUMN_DOWNLOADED
            , DownloadDatabase.COLUMN_TOTAL, DownloadDatabase.COLUMN_STATUS, DownloadDatabase.COLUMN_ERROR
            , DownloadDatabase.COLUMN_NETWORK_TYPE, DownloadDatabase.COLUMN_CREATED, DownloadDatabase.COLUMN_TAG
            , DownloadDatabase.COLUMN_ENQUEUE_ACTION, DownloadDatabase.COLUMN_IDENTIFIER, DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE,
            DownloadDatabase.COLUMN_EXTRAS, DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS, DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS};
        RdbPredicates rdbPredicates = new RdbPredicates(DownloadDatabase.TABLE_NAME);//构建查询谓词
        ResultSet resultSet = database.query(rdbPredicates, columns);
        ArrayList<DownloadInfo> downloadInfos = new ArrayList<>();
        while (resultSet.goToNextRow()) {
            int column_id = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ID));
            String column_namespace = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_NAMESPACE));
            String COLUMN_URL = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_URL));
            String COLUMN_FILE = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_FILE));
            int COLUMN_GROUP = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_GROUP));
            int COLUMN_PRIORITY = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_PRIORITY));
            String COLUMN_HEADERS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_HEADERS));
            long COLUMN_DOWNLOADED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOADED));
            long COLUMN_TOTAL = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TOTAL));
            int COLUMN_STATUS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_STATUS));
            int COLUMN_ERROR = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ERROR));
            long COLUMN_CREATED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_CREATED));
            String COLUMN_TAG = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TAG));
            int COLUMN_ENQUEUE_ACTION = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ENQUEUE_ACTION));
            long COLUMN_IDENTIFIER = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_IDENTIFIER));
            int COLUMN_DOWNLOAD_ON_ENQUEUE = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE));
            String COLUMN_EXTRAS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_EXTRAS));
            int COLUMN_AUTO_RETRY_MAX_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS));
            int COLUMN_AUTO_RETRY_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS));
            if (group == COLUMN_GROUP) {
                DownloadInfo downloadInfo = new DownloadInfo();
                downloadInfo.setId(column_id);
                downloadInfo.setNamespace(column_namespace);
                downloadInfo.setUrl(COLUMN_URL);
                downloadInfo.setFile(COLUMN_FILE);
                downloadInfo.setGroup(COLUMN_GROUP);
                downloadInfo.setPriority(Priority.valueOf(COLUMN_PRIORITY));
                if (COLUMN_HEADERS != null && !COLUMN_HEADERS.equals("")) {
                    HashMap<String, String> headers = new HashMap<>();
                    String[] split = COLUMN_HEADERS.split(DownloadDatabase.CUTTING_LINE);
                    for (int i = 0; i < split.length; i++) {
                        String content = split[i];
                        String[] split1 = content.split(DownloadDatabase.CUTTING_LINE_KEY);
                        String key = split1[0];
                        String value = split1[1];
                        headers.put(key, value);
                    }
                    downloadInfo.setHeaders(headers);
                }
                downloadInfo.setDownloaded(COLUMN_DOWNLOADED);
                downloadInfo.setTotal(COLUMN_TOTAL);
                downloadInfo.setStatus(Status.valueOf(COLUMN_STATUS));
                downloadInfo.setError(Error.valueOf(COLUMN_ERROR));
                downloadInfo.setCreated(COLUMN_CREATED);
                downloadInfo.setTag(COLUMN_TAG);
                downloadInfo.setEnqueueAction(EnqueueAction.valueOf(COLUMN_ENQUEUE_ACTION));
                downloadInfo.setIdentifier(COLUMN_IDENTIFIER);
                if (COLUMN_DOWNLOAD_ON_ENQUEUE == 1) {
                    downloadInfo.setDownloadOnEnqueue(true);
                } else {
                    downloadInfo.setDownloadOnEnqueue(false);
                }
                downloadInfo.setAutoRetryMaxAttempts(COLUMN_AUTO_RETRY_MAX_ATTEMPTS);
                downloadInfo.setAutoRetryAttempts(COLUMN_AUTO_RETRY_ATTEMPTS);
                if (COLUMN_EXTRAS != null && !COLUMN_EXTRAS.equals("")) {
                    ZSONObject zsonObject = ZSONObject.stringToZSON(COLUMN_EXTRAS);
                    Set<String> list = zsonObject.keySet();
                    HashMap<String, String> new_extras = new HashMap<>();
                    for (String key : list) {
                        String value = (String) zsonObject.get(key);
                        new_extras.put(key, value);
                    }
                    downloadInfo.setExtras(new Extras(new_extras));
                }
                downloadInfos.add(downloadInfo);
            }
        }
        resultSet.close();
        return downloadInfos;
    }

    @Override
    public List<DownloadInfo> getByGroupWithStatus(int group, List<Status> statuses) {
        String[] columns = new String[]{DownloadDatabase.COLUMN_ID, DownloadDatabase.COLUMN_NAMESPACE
            , DownloadDatabase.COLUMN_URL, DownloadDatabase.COLUMN_FILE, DownloadDatabase.COLUMN_GROUP
            , DownloadDatabase.COLUMN_PRIORITY, DownloadDatabase.COLUMN_HEADERS, DownloadDatabase.COLUMN_DOWNLOADED
            , DownloadDatabase.COLUMN_TOTAL, DownloadDatabase.COLUMN_STATUS, DownloadDatabase.COLUMN_ERROR
            , DownloadDatabase.COLUMN_NETWORK_TYPE, DownloadDatabase.COLUMN_CREATED, DownloadDatabase.COLUMN_TAG
            , DownloadDatabase.COLUMN_ENQUEUE_ACTION, DownloadDatabase.COLUMN_IDENTIFIER, DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE,
            DownloadDatabase.COLUMN_EXTRAS, DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS, DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS};
        RdbPredicates rdbPredicates = new RdbPredicates(DownloadDatabase.TABLE_NAME);//构建查询谓词
        ResultSet resultSet = database.query(rdbPredicates, columns);
        ArrayList<DownloadInfo> downloadInfos = new ArrayList<>();
        while (resultSet.goToNextRow()) {
            int column_id = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ID));
            String column_namespace = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_NAMESPACE));
            String COLUMN_URL = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_URL));
            String COLUMN_FILE = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_FILE));
            int COLUMN_GROUP = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_GROUP));
            int COLUMN_PRIORITY = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_PRIORITY));
            String COLUMN_HEADERS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_HEADERS));
            long COLUMN_DOWNLOADED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOADED));
            long COLUMN_TOTAL = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TOTAL));
            int COLUMN_STATUS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_STATUS));
            int COLUMN_ERROR = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ERROR));
            long COLUMN_CREATED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_CREATED));
            String COLUMN_TAG = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TAG));
            int COLUMN_ENQUEUE_ACTION = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ENQUEUE_ACTION));
            long COLUMN_IDENTIFIER = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_IDENTIFIER));
            int COLUMN_DOWNLOAD_ON_ENQUEUE = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE));
            String COLUMN_EXTRAS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_EXTRAS));
            int COLUMN_AUTO_RETRY_MAX_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS));
            int COLUMN_AUTO_RETRY_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS));
            for (Status info : statuses) {
                if (group == COLUMN_GROUP && info.getValue() == COLUMN_STATUS) {
                    DownloadInfo downloadInfo = new DownloadInfo();
                    downloadInfo.setId(column_id);
                    downloadInfo.setNamespace(column_namespace);
                    downloadInfo.setUrl(COLUMN_URL);
                    downloadInfo.setFile(COLUMN_FILE);
                    downloadInfo.setGroup(COLUMN_GROUP);
                    downloadInfo.setPriority(Priority.valueOf(COLUMN_PRIORITY));
                    if (COLUMN_HEADERS != null && !COLUMN_HEADERS.equals("")) {
                        HashMap<String, String> headers = new HashMap<>();
                        String[] split = COLUMN_HEADERS.split(DownloadDatabase.CUTTING_LINE);
                        for (int i = 0; i < split.length; i++) {
                            String content = split[i];
                            String[] split1 = content.split(DownloadDatabase.CUTTING_LINE_KEY);
                            String key = split1[0];
                            String value = split1[1];
                            headers.put(key, value);
                        }
                        downloadInfo.setHeaders(headers);
                    }
                    downloadInfo.setDownloaded(COLUMN_DOWNLOADED);
                    downloadInfo.setTotal(COLUMN_TOTAL);
                    downloadInfo.setStatus(Status.valueOf(COLUMN_STATUS));
                    downloadInfo.setError(Error.valueOf(COLUMN_ERROR));
                    downloadInfo.setCreated(COLUMN_CREATED);
                    downloadInfo.setTag(COLUMN_TAG);
                    downloadInfo.setEnqueueAction(EnqueueAction.valueOf(COLUMN_ENQUEUE_ACTION));
                    downloadInfo.setIdentifier(COLUMN_IDENTIFIER);
                    if (COLUMN_DOWNLOAD_ON_ENQUEUE == 1) {
                        downloadInfo.setDownloadOnEnqueue(true);
                    } else {
                        downloadInfo.setDownloadOnEnqueue(false);
                    }
                    downloadInfo.setAutoRetryMaxAttempts(COLUMN_AUTO_RETRY_MAX_ATTEMPTS);
                    downloadInfo.setAutoRetryAttempts(COLUMN_AUTO_RETRY_ATTEMPTS);
                    if (COLUMN_EXTRAS != null && !COLUMN_EXTRAS.equals("")) {
                        ZSONObject zsonObject = ZSONObject.stringToZSON(COLUMN_EXTRAS);
                        Set<String> list = zsonObject.keySet();
                        HashMap<String, String> new_extras = new HashMap<>();
                        for (String key : list) {
                            String value = (String) zsonObject.get(key);
                            new_extras.put(key, value);
                        }
                        downloadInfo.setExtras(new Extras(new_extras));
                    }
                    downloadInfos.add(downloadInfo);
                }
            }
        }
        resultSet.close();
        return downloadInfos;
    }

    @Override
    public List<DownloadInfo> getPendingDownloadsSorted(Status status) {
        List<DownloadInfo> byStatus = getByStatus(status);
        Collections.sort(byStatus, new Comparator<DownloadInfo>() {
            @Override
            public int compare(DownloadInfo downloadInfo, DownloadInfo t1) {
                return downloadInfo.getStatus().getValue() - t1.getStatus().getValue();
            }
        });
        return byStatus;
    }

    @Override
    public List<DownloadInfo> getPendingDownloadsSortedDesc(Status status) {
        List<DownloadInfo> byStatus = getByStatus(status);
        Collections.sort(byStatus, new Comparator<DownloadInfo>() {
            @Override
            public int compare(DownloadInfo downloadInfo, DownloadInfo t1) {
                return t1.getStatus().getValue() - downloadInfo.getStatus().getValue();
            }
        });
        return byStatus;
    }

    @Override
    public List<DownloadInfo> getDownloadsByRequestIdentifier(long identifier) {
        String[] columns = new String[]{DownloadDatabase.COLUMN_ID, DownloadDatabase.COLUMN_NAMESPACE
            , DownloadDatabase.COLUMN_URL, DownloadDatabase.COLUMN_FILE, DownloadDatabase.COLUMN_GROUP
            , DownloadDatabase.COLUMN_PRIORITY, DownloadDatabase.COLUMN_HEADERS, DownloadDatabase.COLUMN_DOWNLOADED
            , DownloadDatabase.COLUMN_TOTAL, DownloadDatabase.COLUMN_STATUS, DownloadDatabase.COLUMN_ERROR
            , DownloadDatabase.COLUMN_NETWORK_TYPE, DownloadDatabase.COLUMN_CREATED, DownloadDatabase.COLUMN_TAG
            , DownloadDatabase.COLUMN_ENQUEUE_ACTION, DownloadDatabase.COLUMN_IDENTIFIER, DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE,
            DownloadDatabase.COLUMN_EXTRAS, DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS, DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS};
        RdbPredicates rdbPredicates = new RdbPredicates(DownloadDatabase.TABLE_NAME);//构建查询谓词
        ResultSet resultSet = database.query(rdbPredicates, columns);
        ArrayList<DownloadInfo> downloadInfos = new ArrayList<>();
        while (resultSet.goToNextRow()) {
            int column_id = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ID));
            String column_namespace = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_NAMESPACE));
            String COLUMN_URL = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_URL));
            String COLUMN_FILE = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_FILE));
            int COLUMN_GROUP = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_GROUP));
            int COLUMN_PRIORITY = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_PRIORITY));
            String COLUMN_HEADERS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_HEADERS));
            long COLUMN_DOWNLOADED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOADED));
            long COLUMN_TOTAL = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TOTAL));
            int COLUMN_STATUS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_STATUS));
            int COLUMN_ERROR = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ERROR));
            long COLUMN_CREATED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_CREATED));
            String COLUMN_TAG = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TAG));
            int COLUMN_ENQUEUE_ACTION = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ENQUEUE_ACTION));
            long COLUMN_IDENTIFIER = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_IDENTIFIER));
            int COLUMN_DOWNLOAD_ON_ENQUEUE = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE));
            String COLUMN_EXTRAS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_EXTRAS));
            int COLUMN_AUTO_RETRY_MAX_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS));
            int COLUMN_AUTO_RETRY_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS));
            if (identifier == COLUMN_IDENTIFIER) {
                DownloadInfo downloadInfo = new DownloadInfo();
                downloadInfo.setId(column_id);
                downloadInfo.setNamespace(column_namespace);
                downloadInfo.setUrl(COLUMN_URL);
                downloadInfo.setFile(COLUMN_FILE);
                downloadInfo.setGroup(COLUMN_GROUP);
                downloadInfo.setPriority(Priority.valueOf(COLUMN_PRIORITY));
                if (COLUMN_HEADERS != null && !COLUMN_HEADERS.equals("")) {
                    HashMap<String, String> headers = new HashMap<>();
                    String[] split = COLUMN_HEADERS.split(DownloadDatabase.CUTTING_LINE);
                    for (int i = 0; i < split.length; i++) {
                        String content = split[i];
                        String[] split1 = content.split(DownloadDatabase.CUTTING_LINE_KEY);
                        String key = split1[0];
                        String value = split1[1];
                        headers.put(key, value);
                    }
                    downloadInfo.setHeaders(headers);
                }
                downloadInfo.setDownloaded(COLUMN_DOWNLOADED);
                downloadInfo.setTotal(COLUMN_TOTAL);
                downloadInfo.setStatus(Status.valueOf(COLUMN_STATUS));
                downloadInfo.setError(Error.valueOf(COLUMN_ERROR));
                downloadInfo.setCreated(COLUMN_CREATED);
                downloadInfo.setTag(COLUMN_TAG);
                downloadInfo.setEnqueueAction(EnqueueAction.valueOf(COLUMN_ENQUEUE_ACTION));
                downloadInfo.setIdentifier(COLUMN_IDENTIFIER);
                if (COLUMN_DOWNLOAD_ON_ENQUEUE == 1) {
                    downloadInfo.setDownloadOnEnqueue(true);
                } else {
                    downloadInfo.setDownloadOnEnqueue(false);
                }
                downloadInfo.setAutoRetryMaxAttempts(COLUMN_AUTO_RETRY_MAX_ATTEMPTS);
                downloadInfo.setAutoRetryAttempts(COLUMN_AUTO_RETRY_ATTEMPTS);
                if (COLUMN_EXTRAS != null && !COLUMN_EXTRAS.equals("")) {
                    ZSONObject zsonObject = ZSONObject.stringToZSON(COLUMN_EXTRAS);
                    Set<String> list = zsonObject.keySet();
                    HashMap<String, String> new_extras = new HashMap<>();
                    for (String key : list) {
                        String value = (String) zsonObject.get(key);
                        new_extras.put(key, value);
                    }
                    downloadInfo.setExtras(new Extras(new_extras));
                }
                downloadInfos.add(downloadInfo);
            }
        }
        resultSet.close();
        return downloadInfos;
    }

    @Override
    public List<DownloadInfo> getDownloadsByTag(String tag) {
        String[] columns = new String[]{DownloadDatabase.COLUMN_ID, DownloadDatabase.COLUMN_NAMESPACE
            , DownloadDatabase.COLUMN_URL, DownloadDatabase.COLUMN_FILE, DownloadDatabase.COLUMN_GROUP
            , DownloadDatabase.COLUMN_PRIORITY, DownloadDatabase.COLUMN_HEADERS, DownloadDatabase.COLUMN_DOWNLOADED
            , DownloadDatabase.COLUMN_TOTAL, DownloadDatabase.COLUMN_STATUS, DownloadDatabase.COLUMN_ERROR
            , DownloadDatabase.COLUMN_NETWORK_TYPE, DownloadDatabase.COLUMN_CREATED, DownloadDatabase.COLUMN_TAG
            , DownloadDatabase.COLUMN_ENQUEUE_ACTION, DownloadDatabase.COLUMN_IDENTIFIER, DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE,
            DownloadDatabase.COLUMN_EXTRAS, DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS, DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS};
        RdbPredicates rdbPredicates = new RdbPredicates(DownloadDatabase.TABLE_NAME);//构建查询谓词
        ResultSet resultSet = database.query(rdbPredicates, columns);
        ArrayList<DownloadInfo> downloadInfos = new ArrayList<>();
        while (resultSet.goToNextRow()) {
            int column_id = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ID));
            String column_namespace = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_NAMESPACE));
            String COLUMN_URL = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_URL));
            String COLUMN_FILE = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_FILE));
            int COLUMN_GROUP = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_GROUP));
            int COLUMN_PRIORITY = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_PRIORITY));
            String COLUMN_HEADERS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_HEADERS));
            long COLUMN_DOWNLOADED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOADED));
            long COLUMN_TOTAL = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TOTAL));
            int COLUMN_STATUS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_STATUS));
            int COLUMN_ERROR = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ERROR));
            long COLUMN_CREATED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_CREATED));
            String COLUMN_TAG = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TAG));
            int COLUMN_ENQUEUE_ACTION = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ENQUEUE_ACTION));
            long COLUMN_IDENTIFIER = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_IDENTIFIER));
            int COLUMN_DOWNLOAD_ON_ENQUEUE = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE));
            String COLUMN_EXTRAS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_EXTRAS));
            int COLUMN_AUTO_RETRY_MAX_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS));
            int COLUMN_AUTO_RETRY_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS));
            if (tag.equals(COLUMN_TAG)) {
                DownloadInfo downloadInfo = new DownloadInfo();
                downloadInfo.setId(column_id);
                downloadInfo.setNamespace(column_namespace);
                downloadInfo.setUrl(COLUMN_URL);
                downloadInfo.setFile(COLUMN_FILE);
                downloadInfo.setGroup(COLUMN_GROUP);
                downloadInfo.setPriority(Priority.valueOf(COLUMN_PRIORITY));
                if (COLUMN_HEADERS != null && !COLUMN_HEADERS.equals("")) {
                    HashMap<String, String> headers = new HashMap<>();
                    String[] split = COLUMN_HEADERS.split(DownloadDatabase.CUTTING_LINE);
                    for (int i = 0; i < split.length; i++) {
                        String content = split[i];
                        String[] split1 = content.split(DownloadDatabase.CUTTING_LINE_KEY);
                        String key = split1[0];
                        String value = split1[1];
                        headers.put(key, value);
                    }
                    downloadInfo.setHeaders(headers);
                }
                downloadInfo.setDownloaded(COLUMN_DOWNLOADED);
                downloadInfo.setTotal(COLUMN_TOTAL);
                downloadInfo.setStatus(Status.valueOf(COLUMN_STATUS));
                downloadInfo.setError(Error.valueOf(COLUMN_ERROR));
                downloadInfo.setCreated(COLUMN_CREATED);
                downloadInfo.setTag(COLUMN_TAG);
                downloadInfo.setEnqueueAction(EnqueueAction.valueOf(COLUMN_ENQUEUE_ACTION));
                downloadInfo.setIdentifier(COLUMN_IDENTIFIER);
                if (COLUMN_DOWNLOAD_ON_ENQUEUE == 1) {
                    downloadInfo.setDownloadOnEnqueue(true);
                } else {
                    downloadInfo.setDownloadOnEnqueue(false);
                }
                downloadInfo.setAutoRetryMaxAttempts(COLUMN_AUTO_RETRY_MAX_ATTEMPTS);
                downloadInfo.setAutoRetryAttempts(COLUMN_AUTO_RETRY_ATTEMPTS);
                if (COLUMN_EXTRAS != null && !COLUMN_EXTRAS.equals("")) {
                    ZSONObject zsonObject = ZSONObject.stringToZSON(COLUMN_EXTRAS);
                    Set<String> list = zsonObject.keySet();
                    HashMap<String, String> new_extras = new HashMap<>();
                    for (String key : list) {
                        String value = (String) zsonObject.get(key);
                        new_extras.put(key, value);
                    }
                    downloadInfo.setExtras(new Extras(new_extras));
                }
                downloadInfos.add(downloadInfo);
            }
        }
        resultSet.close();
        return downloadInfos;
    }

    @Override
    public List<Integer> getAllGroupIds() {
        String[] columns = new String[]{DownloadDatabase.COLUMN_ID, DownloadDatabase.COLUMN_NAMESPACE
            , DownloadDatabase.COLUMN_URL, DownloadDatabase.COLUMN_FILE, DownloadDatabase.COLUMN_GROUP
            , DownloadDatabase.COLUMN_PRIORITY, DownloadDatabase.COLUMN_HEADERS, DownloadDatabase.COLUMN_DOWNLOADED
            , DownloadDatabase.COLUMN_TOTAL, DownloadDatabase.COLUMN_STATUS, DownloadDatabase.COLUMN_ERROR
            , DownloadDatabase.COLUMN_NETWORK_TYPE, DownloadDatabase.COLUMN_CREATED, DownloadDatabase.COLUMN_TAG
            , DownloadDatabase.COLUMN_ENQUEUE_ACTION, DownloadDatabase.COLUMN_IDENTIFIER, DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE,
            DownloadDatabase.COLUMN_EXTRAS, DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS, DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS};
        RdbPredicates rdbPredicates = new RdbPredicates(DownloadDatabase.TABLE_NAME);//构建查询谓词
        ResultSet resultSet = database.query(rdbPredicates, columns);
        ArrayList<Integer> downloadInfos = new ArrayList<>();
        while (resultSet.goToNextRow()) {
            int column_id = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ID));
            String column_namespace = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_NAMESPACE));
            String COLUMN_URL = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_URL));
            String COLUMN_FILE = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_FILE));
            int COLUMN_GROUP = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_GROUP));
            int COLUMN_PRIORITY = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_PRIORITY));
            String COLUMN_HEADERS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_HEADERS));
            long COLUMN_DOWNLOADED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOADED));
            long COLUMN_TOTAL = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TOTAL));
            int COLUMN_STATUS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_STATUS));
            int COLUMN_ERROR = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ERROR));
            long COLUMN_CREATED = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_CREATED));
            String COLUMN_TAG = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_TAG));
            int COLUMN_ENQUEUE_ACTION = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_ENQUEUE_ACTION));
            long COLUMN_IDENTIFIER = resultSet.getLong(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_IDENTIFIER));
            int COLUMN_DOWNLOAD_ON_ENQUEUE = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_DOWNLOAD_ON_ENQUEUE));
            String COLUMN_EXTRAS = resultSet.getString(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_EXTRAS));
            int COLUMN_AUTO_RETRY_MAX_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_MAX_ATTEMPTS));
            int COLUMN_AUTO_RETRY_ATTEMPTS = resultSet.getInt(resultSet.getColumnIndexForName(DownloadDatabase.COLUMN_AUTO_RETRY_ATTEMPTS));
            if (!downloadInfos.contains(column_id)) {
                downloadInfos.add(column_id);
            }
        }
        resultSet.close();
        return downloadInfos;
    }
}
